<!--
 * @Description: text文档导出
 * @Date: 2022-06-27 16:08:21
 * @LastEditTime: 2022-06-28 09:51:37
 * @FilePath: /测试文档(总结)/41.text文档导出.html
 * @Author: byhzdemacbook-pro-2.local
-->
<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>

<body>
    <textarea id="text"></textarea>
    <button onclick="saveFile1()">导出输入数据</button>
    <button onclick="saveFile2()">导出自定义数据</button>
</body>

</html>
<script type="text/javascript">
    function saveFile1() {
        // 导出数据'自定义的数据内容，可以是服务端返回的！';
        //手动输入数据
        let data1 = document.querySelector('#text').value;
        let name1 = 'data1.txt';//定义文件名称
        exportRaw(data1, name1);
    }
    function saveFile2() {
        // 导出数据'自定义的数据内容，可以是服务端返回的！';
        //直接给定的值
        let data2 = JSON.stringify([
            {
                "point ": 1,
                value: 123
            },
            {
                "point ": 2,
                value: 1234
            },
            {
                "point ": 1,
                value: 123
            },
            {
                "point ": 2,
                value: 1234
            }
        ])
        // let data3 = "dsdsdsd"
        let name2 = '测试.txt';//定义文件名称
        exportRaw(data2, name2 );
    }
    function exportRaw(data, name) {
        let urlObject = window.URL || window.webkitURL || window;
        let export_blob = new Blob([data]);
        let save_link = document.createElement('a')
        // let save_link = document.createElementNS("http://www.w3.org/1999/xhtml", "a")
        save_link.href = urlObject.createObjectURL(export_blob);
        save_link.download = name;
        save_link.click();
    }

</script>